import cryptoJS from 'crypto-js';

const key = cryptoJS.enc.Utf8.parse('123456789789hnnd');
const iv = cryptoJS.enc.Utf8.parse('123456789789hnnd');

//aes加密
export function codeAes(data: any) {
  const src = cryptoJS.enc.Utf8.parse(data);
  const encrypted = cryptoJS.AES.encrypt(src, key, {
    iv,
    mode: cryptoJS.mode.CBC,
    padding: cryptoJS.pad.Pkcs7,
  });
  return encrypted.ciphertext.toString().toUpperCase();
}

//aes解密
export function unCodeAes(data: any) {
  const enc = cryptoJS.enc.Hex.parse(data);
  const src = cryptoJS.enc.Base64.stringify(enc);
  const decrpt = cryptoJS.AES.decrypt(src, key, {
    iv,
    mode: cryptoJS.mode.CBC,
    padding: cryptoJS.pad.Pkcs7,
  });
  const decrptStr = decrpt.toString(cryptoJS.enc.Utf8);
  return decrptStr.toString();
}
